<?php
class model_games extends base_model
{
    public function mod_games($args){
        $where_txt = "";
        if(!empty($args['sKey'])){
            $where_txt .= " AND `title` LIKE '%".$args['sKey']."%'";
        }

        $sql = "SELECT ~field~ FROM `@@games` WHERE 1 {$where_txt} ~order~ ~limit~";
        //取出当前页的数据
        $sql_arr = array(
            'field' => '*',
            'page_current' => $args['page_current'],
            'pagesize' => $args['ps'],
            'order' => ' ORDER BY `sort` ASC',
        );
        $datas = $this->getTableRows($sql,$sql_arr);
        return array(
            'datas' => $datas['datas'],
            'record' => $datas['total'],
        );
    }
    public function mod_games_odds($gameid){
        $where_txt = "gameid=".intval($gameid);
        $sql = "SELECT `id`,`gameid`,`cardtitle`,`manymen`,`cardtype`,`winsort`,`odds`,`winerprofit`,`ticket`,`state` FROM `@@games_odds` WHERE {$where_txt} ORDER BY `winsort` DESC";
        //取出当前页的数据
        return $this->db->query($sql,3);
    }
    public function mod_autogrob($gameid)
    {
        $sql = "SELECT * FROM `@@bets` WHERE `gameid`=".$gameid." AND `state`=0 AND UNIX_TIMESTAMP() - `begintimes` <=180 AND `pass`='' ORDER BY `coin` ASC LIMIT 1";
        $data = $this->db->query($sql,2);
        return $data;
    }
    public function mod_autogrobuser($red)
    {
        if($red['usertype']==0){
            $usertype = ' AND U.`usertype`=0';
        }else{
            $usertype = ' AND U.`usertype`>=1';
        }
        # 找出已参与的玩家
        $sql = "SELECT GROUP_CONCAT(uid) AS _UIDS FROM `@@betslog` WHERE `gameid`=".$red['gameid']." and `issue`='".$red['issue']."'";
        $grabuser = $this->db->query($sql,2);
        $uids = $grabuser['_UIDS'];
        $sql = 'SELECT U.* FROM `@@autosend` A,`@@users` U WHERE A.`uid`=U.`uid` AND A.`uid` NOT IN('.$uids.') AND A.`gameid`='.$red['gameid'].' '.$usertype;
        $data = $this->db->query($sql,3);
        $user = array_rand($data, 1);
        $data = $data[$user];
        if($data){
            $update = $this->mod_updatetoken($data['uid'],$data['uno'],$data['token']);
            $data['webtoken'] = $update['webtoken'];
            $data['lastlogintimes'] = $update['lastlogintimes'];
        }
        return $data;
    }
    public function mod_updatetoken($uid,$uno,$token)
    {
        # 更新用户的token
        $token = $this->settoken($uno, $token);
        $times = time();
        $sql = "UPDATE `@@users` SET `webtoken`='".$token."', `lastlogintimes`=".$times." WHERE `uid`=".$uid." LIMIT 1";
        $this->db->query($sql, 1);
        return array(
            'webtoken' => $token,
            'lastlogintimes' => time(),
        );
    }
    # region 取游戏的参数不包括赔率
    /**
     * 取游戏的参数不包括赔率
     * @param $gameid
     * @return mixed
     */
    public function mod_getGameForID($gameid)
    {
        $redis_key = 'games_item_forid_'.$gameid;
        if($this->redis->exists($redis_key))
        {
            $data = $this->redisGet($redis_key);
        }else {
            $where = "`id`={$gameid}";
            $fields = "id, title, func, establishcost, ishavebanker, establishcoin, manymen, custombetscoin, betscoinlist, minbetcoin, maxbetcoin, overtime, maxodds, state, passcoin";
            $tablename = "@@games";
            $sql = "SELECT {$fields} FROM `{$tablename}` WHERE {$where}";
            $data = $this->db->querty($sql,2);
            $this->redisSet($redis_key, $data);
        }
        return $data;
    }
    # endregion
}